<?php
/**
 * V7 checked 1
 * 
 * @author Leon
 *
 */
class data_save_article extends data_article {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 写入saved article
	 * 写入content，写入之前做了content clip的第一步检查
	 * 写入circle article
	 * 
	 * 		发布event:hight 增加url计数，增加circle统计数，增加我在圈子中发布的文章数，发布到recommend article，发布feed，发布社交网站信息
	 *
	 * @param int $uid
	 * @param string $url
	 * @param string $title					// 没有转义的
	 * @param string $content				// 没有转义的
	 * @param string $friends
	 * @param string $circles
	 * @return int
	 */
	public function add ($uid, $url, $title, $content, $friends, $circles) {
		$uid 		= intval($uid);
		$url		= url_cut_tail($url);
		$url_id 	= $this->url_to_id_or_insert($url);
		$title 		= addslashes(strip_tags(trim($title)));
		$friends	= format_id_string($friends, false);		// 整理id字符串，返回字符串
		$circles	= format_id_string($circles, true);		// 整理id字符串，返回数组
		
		// 检查圈子是否是成员，圈子是否是public，如果有一个是public即可发布feed
		$data_circle = new data_circle();
		if (is_array($circles)) {
			foreach ($circles as $circle_id) {
				if ($data_circle->is_member($uid, $circle_id)) {
					$checked_circles[] = $circle_id;
				}
			}
		}
		
		if ($uid and $url_id and $title and is_array($checked_circles)) {
			$insert_id = $this->db->execute('
				insert into
					save_article
				set
					user_id	= "'.$uid.'",
					url_id		= "'.$url_id.'",
					title		= "'.$title.'",
					dt		= "'.date('Y-m-d H:i:s').'"
			');
			if ($insert_id) {
				// 写入content，进行content_clip的第一步整理：替换了文章中的link和img标签为标准绝对路径，对img做了标记并且去除了换行和空白，去掉了script、注释、iframe、form标签、input标签
				$content_clip = new content_clip();
				$content = $content_clip->replace_for_save($url, $content);
				$this->set_save_article_content($insert_id, $content);
				// 发布到circle
				$sql = '
					insert into
						circle_article
					(circle_id, article_id, author_id, url_id, dt, insite_article_id)
						values
				';
				$dt = date('Y-m-d H:i:s');
				foreach ($checked_circles as $circle_id) {
					$sql .= '("'.$circle_id.'", "'.$insert_id.'", "'.$uid.'", "'.$url_id.'", "'.$dt.'", 0),';
				}
				$sql = substr($sql, 0, -1);	// 去掉最后一个“,”
				$insert_into_circle = $this->db->execute($sql);
				if ($insert_into_circle) {
					// 发布event:hight 发布到recommend article，发布feed，增加我在圈子中发布的文章数，增加circle统计数，增加url计数，发布社交网站信息
					$event_param = array (
						'type'				=> 'outsite',
						'user_id'			=> $uid,
						'article_id'			=> $insert_id,
						'url_id'			=> $url_id,
						'unchecked_friend_ids'	=> $friends,
						'checked_circle_ids'	=> implode(',', $checked_circles)
					);
					$data_event = new data_event('save_article', $event_param, 'high');

					return $insert_id;
				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 站内存档
	 *
	 * @param int $uid
	 * @param string $article_type		// circle / feed / recommend / hot (save / save_on_edit 这两个应该不存在，是自己存档的)
	 * @param int $id				// 相应的主键id
	 * @param string $friends
	 * @param string $circles
	 * @return int
	 */
	public function in_site_save($uid, $article_type, $id, $friends, $circles) {
		$uid		= intval($uid);
		$id		= intval($id);
		$friends	= format_id_string($friends, false);
		$circles	= format_id_string($circles, true);
		
		// 检查圈子是否是成员
		$data_circle = new data_circle();
		foreach ($circles as $circle_id) {
			if ($data_circle->is_member($uid, $circle_id)) {
				$checked_circles[] = $circle_id;
			}
		}
		
		if ($uid and $id and is_array($checked_circles)) {
			switch ($article_type) {
				case 'circle':
					$article = $this->one_in_circle($uid, $id);
					break;
				case 'feed':
					$data_feed = new data_feed();
					$article = $data_feed->one_article($uid, $id);
					break;
				case 'recommend':
					$data_recommend_article = new data_recommend_article();
					$article = $data_recommend_article->one($uid, $id);
					break;
				case 'hot':
					$data_guest = new data_guest();
					$article = $data_guest->one_article($id);
					break;
				default:
					return false;
			}
			if ($article) {
				$insert_id = $this->db->execute('
					insert into
						save_article
					(user_id, url_id, title, dt, is_clip_over, summary, img_0, img_1, img_2, is_checked_by_admin, special_flag)
						select
							"'.$uid.'", 
							url_id,
							title,
							"'.date('Y-m-d H:i:s').'",
							is_clip_over,
							summary,
							img_0,
							img_1,
							img_2,
							is_checked_by_admin,
							special_flag
						from 
							save_article 
						where 
							id = "'.$article['article_id'].'"
				');

				if ($insert_id) {
					// copy content and clip
					$this->copy_content_to_save($insert_id, $article['content_id']);
					
					// 检查原来的article id已经发布在哪些圈子里了，已经存在的在后面标记insite_article_id
					$exist = $this->db->execute('
						select
							circle_id
						from
							circle_article
						where
							article_id	= "'.$article['article_id'].'"
					');
					if (is_array($exist)) {
						foreach ($exist as $v) {
							$basket[] = $v['circle_id'];
						}
					} else {
						$basket = array();		// 这种情况应该不可能存在
					}
					
					// 发布到circle
					$sql = '
						insert into
							circle_article
						(circle_id, article_id, author_id, url_id, dt, insite_article_id)
							values
					';
					$dt = date('Y-m-d H:i:s');
					foreach ($checked_circles as $circle_id) {
						if (array_search($circle_id, $basket) === false) {
							$sql .= '("'.$circle_id.'", "'.$insert_id.'", "'.$uid.'", "'.$article['url_id'].'", "'.$dt.'", 0),';
						} else {
							$sql .= '("'.$circle_id.'", "'.$insert_id.'", "'.$uid.'", "'.$article['url_id'].'", "'.$dt.'", "'.$article['article_id'].'"),';
						}
					}
					$sql = substr($sql, 0, -1);
					$this->db->execute($sql);
					
					// 发布event:low  发布到recommend article，增加我在圈子中发布的文章数，增加circle统计数，增加url计数，发布社交网站信息
					
					$event_param = array (
						'type'				=> 'insite_save',
						'user_id'			=> $uid,
						'article_id'			=> $insert_id,
						'url_id'			=> $article['url_id'],
						'unchecked_friend_ids'	=> $friends,
						'checked_circle_ids'	=> implode(',', $checked_circles),
					);
					$data_event = new data_event('save_article', $event_param, 'low');

					return $insert_id;
				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 
	 * @param type $uid
	 * @param type $article_id
	 * @param type $friends		// 允许friends为空
	 * @param type $circles			// 允许circles为空
	 * @return boolean
	 */
	public function in_site_share ($uid, $article_id, $friends, $circles) {
		$uid		= intval($uid);
		$article_id	= intval($article_id);
		$friends	= format_id_string($friends, false);		// 返回字符串
		$circles	= format_id_string($circles, true);		// 返回数组
		
		if ($uid and $article_id) {
			$is_my_article = $this->db->execute('
				select
					*
				from
					save_article
				where
					id		= "'.$article_id.'"		and
					user_id	= "'.$uid.'"
			');
			if ($is_my_article) {
				// 检查圈子是否是成员，不用发布feed
				$data_circle = new data_circle();
				if (is_array($circles)) {
					foreach ($circles as $circle_id) {
						if ($data_circle->is_member($uid, $circle_id)) {
							$checked_circles[] = $circle_id;
						}
					}
				}
				// 至少存在圈子或者好友时有效
				if (is_array($checked_circles) or $friends) {
					if (is_array($checked_circles)) {
						// 发布到circle
						$sql = '
							insert ignore into
								circle_article
							(circle_id, article_id, author_id, url_id, dt, insite_article_id)
								values
						';
						$dt = date('Y-m-d H:i:s');
						foreach ($checked_circles as $circle_id) {
							$sql .= '("'.$circle_id.'", "'.$is_my_article[0]['id'].'", "'.$uid.'", "'.$is_my_article[0]['url_id'].'", "'.$dt.'", 0),';
						}
						$sql = substr($sql, 0, -1);
						$this->db->execute($sql);
					}

					// 发布event:low  发布到recommend article，增加我在圈子中发布的文章数，增加circle统计数，增加url计数，发布社交网站信息
					$event_param = array (
						'type'				=> 'insite_share',
						'user_id'			=> $uid,
						'article_id'			=> $is_my_article[0]['id'],
						'url_id'			=> $is_my_article[0]['url_id'],
						'unchecked_friend_ids'	=> $friends,
						'checked_circle_ids'	=> ((is_array($checked_circles)) ? implode(',', $checked_circles) : ''),
					);
					$data_event = new data_event('save_article', $event_param, 'low');

					return $article_id;
				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 修改存档文章的标题和正文
	 *	写入content之前做了content clip的第一步检查
	 *
	 * @param int $uid
	 * @param int $pkid
	 * @param String $title				// 没有经过任何转义
	 * @param String $content			// 没有经过任何转义
	 * @return bool
	 */
	public function edit ($uid, $pkid, $title, $content) {
		$uid		= intval($uid);
		$pkid		= intval($pkid);
		$title		= addslashes(strip_tags(trim($title)));
		
		if ($uid and $pkid and $title and $content and $this->db->execute('select id from save_article where id="'.$pkid.'" and user_id="'.$uid.'"')) {
			// 更新标题
			$update_title = $this->db->execute('
				update
					save_article
				set
					title		= "'.$title.'"
				where
					id		= "'.$pkid.'"	and
					user_id	= "'.$uid.'"
			');
			// 更新正文，进行content_clip的第一步整理：替换了文章中的link和img标签为标准绝对路径，对img做了标记并且去除了换行和空白，去掉了script、注释、iframe、form标签、input标签
			// 查找url，为了整理正文
			$article = $this->db->execute('
				select
					article_url.url
				from
					save_article, article_url
				where
					save_article.id	= "'.$pkid.'"		and
					article_url.id	= save_article.url_id
			');
			$content_clip = new content_clip();
			$content = $content_clip->replace_for_save($article[0]['url'], $content);
			
			$update_content = $this->set_content_clip($pkid, $content);		// 编辑完成的内容放在clip中
			
			// 标记clip，需要重新分析clip，检查大图、提取摘要和大图
			$this->set_is_clip_over($pkid, 3);

			return ($update_title or $update_content);
		} else return false;
	}
	/**
	 * 列出我的阅读圈中的所有文章
	 * 		列出文章时，把文章作者的昵称转换为好友昵称
	 *
	 * @param int $uid				// uid可以为0
	 * @param int $circle_id			// 0:全部文章；>0:指定圈子中的文章（检查权限）
	 * @param int $page				// 最小为1
	 * @param int $limit				// 最小为1
	 * @param bool $is_one_more_read
	 * @return Array
	 */
	public function ls_tee ($uid, $circle_id=0, $page=1, $limit=20, $is_one_more_read=false) {
		$uid 		= intval($uid);
		$page	= ($page>1) ? intval($page) : 1;
		$limit		= ($limit>1) ? intval($limit) : 1;
		$circle_id	= intval($circle_id);
		
		if ($is_one_more_read) {
			$limit = ' limit ' . (($page-1)*$limit) . ', ' . ($limit+1);		// 多读一条用于判断是否有下一页
		} else {
			$limit = ' limit ' . (($page-1)*$limit) . ', ' . $limit;
		}
		
		if ($circle_id > 0) {	// 检查circle权限
			$data_circle = new data_circle();
			if (!$data_circle->get_access($uid, $circle_id)) {		// 如果没有访问权限，则重置circle id
				$circle_id = 0;
			}
		}
		
		if ($circle_id > 0) {							// 指定圈子的文章，检查权限
			$execute = $this->db->execute('
				select
					circle_article.id		AS id,
					save_article.id		AS article_id,
					save_article.user_id	AS user_id,
					save_article.url_id		AS url_id,
					save_article.title		AS title,
					circle_article.dt		AS dt,
					article_url.url		AS url,
					save_article.summary	AS summary,
					save_article.img_0		AS img_0,
					save_article.img_1		AS img_1,
					save_article.img_2		AS img_2
				from
					circle_article, save_article, article_url
				where
					circle_article.circle_id		= "'.$circle_id.'" 			and
					circle_article.insite_article_id	= 0					and
					save_article.id			= circle_article.article_id		and
					article_url.id			= save_article.url_id
				order by
					circle_article.id DESC
				'.$limit.'
			');
		} else {				// 用户uid的全部文章
			$execute = $this->db->execute('
				select
					circle_article.id		AS id,
					save_article.id		AS article_id,
					save_article.user_id	AS user_id,
					save_article.url_id		AS url_id,
					save_article.title		AS title,
					circle_article.dt		AS dt,
					article_url.url		AS url,
					save_article.summary	AS summary,
					save_article.img_0		AS img_0,
					save_article.img_1		AS img_1,
					save_article.img_2		AS img_2
				from
					circle_member, circle_article, save_article, article_url
				where
					circle_member.user_id		= "'.$uid.'"				and
					circle_article.circle_id		= circle_member.circle_id	and
					circle_article.insite_article_id	= 0					and
					save_article.id			= circle_article.article_id		and
					article_url.id			= save_article.url_id
				group by
					save_article.id
				order by
					circle_article.id DESC
				'.$limit.'
			');
		}
		
		if (is_array($execute)) {
			$data_user = new data_user();
			foreach ($execute as $k=>$v) {
				$execute[$k]['user_nickname'] = $data_user->friend_to_nickname($uid, $v['user_id']);
				$execute[$k]['circles'] = $this->db->execute('
					select
						circle.id,
						circle.title
					from
						circle_article, circle_member, circle
					where
						circle_article.article_id		= "'.$v['article_id'].'"		and
						circle_member.user_id		= "'.$uid.'"				and
						circle_member.circle_id		= circle_article.circle_id		and
						circle.id				= circle_article.circle_id	
				');
			}
			return $execute;
		} else return false;
	}
	/**
	 * 删除圈子circle_article中的文章
	 * 		发布event
	 *
	 * @param int $uid					// 操作者的uid
	 * @param int $circle_id				// 圈子id
	 * @param string $ids					// 删除的主键id, circle_article表的主键id
	 * @return bool
	 */
	public function del_in_circle ($uid, $circle_id, $ids) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		$ids		= format_id_string($ids, false);			// 返回字符串
		
		if ($uid and $circle_id and $ids) {
			// 检查uid对于circle是否有权限
			$data_circle = new data_circle();
			if ($data_circle->is_admin($uid, $circle_id)) {
				$del = $this->db->execute('
					delete from 
						circle_article 
					where 
						id 		IN 	('.$ids.') 		and 
						circle_id	=	"'.$circle_id.'"
				');
				 //统计圈内文章总数
				 $data_circle->count_article($circle_id);
				 
				 return $del;
			} else return false;
		} else return false;
	}
	/**
	 * 列出我的存档文章，只有自己有权限列出
	 *
	 * @param int $uid				// 自己的id
	 * @param int $circle_id			// 0：全部自己的存档；>0：指定圈子中自己的存档文章
	 * @param int $page
	 * @param int $limit
	 * @return array
	 */
	public function ls_saved_by_me ($uid, $circle_id, $page=1, $limit=20) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		$page	= ($page>1)  ? intval($page)  : 1;
		$limit		= ($limit>1) ? intval($limit) : 1;
		
		if ($uid) {
			if ($circle_id > 0) {			// 从circle article表中查询
				$sql = 'select
						save_article.id		AS id,
						save_article.id		AS article_id,
						save_article.user_id	AS user_id,
						save_article.url_id		AS url_id,
						save_article.title		AS title,
						save_article.dt		AS dt,
						save_article.summary	AS summary,
						save_article.img_0		AS img_0,
						save_article.img_1		AS img_1,
						save_article.img_2		AS img_2,
						article_url.url		AS url
					from
						circle_article, save_article, article_url
					where
						circle_article.author_id	= "'.$uid.'"				and
						circle_article.circle_id	= "'.$circle_id.'"			and
						save_article.id		= circle_article.article_id		and
						article_url.id		= save_article.url_id
					order by
						save_article.id DESC
					limit
						'.(($page-1)*$limit).', '.$limit.'
				';
			} else {						// 从save article表中查询
				$sql = 'select
						save_article.id		AS id,
						save_article.id		AS article_id,
						save_article.user_id	AS user_id,
						save_article.url_id		AS url_id,
						save_article.title		AS title,
						save_article.dt		AS dt,
						save_article.summary	AS summary,
						save_article.img_0		AS img_0,
						save_article.img_1		AS img_1,
						save_article.img_2		AS img_2,
						article_url.url		AS url
					from
						save_article, article_url
					where
						save_article.user_id	= "'.$uid.'"				and
						article_url.id		= save_article.url_id
					order by
						save_article.id DESC
					limit
						'.(($page-1)*$limit).', '.$limit.'
				';
			}
			$execute = $this->db->execute($sql);
			if (is_array($execute)) {
				foreach ($execute as $k=>$v) {
					$execute[$k]['circles'] = $this->db->execute('
						select
							circle.id,
							circle.title
						from
							circle_article, circle
						where
							circle_article.article_id 	= "'.$v['id'].'"			and
							circle.id			= circle_article.circle_id
					');
				}
			}
			return $execute;
		} else return false;
	}
	/**
	 * 删除存档文章
	 * 		删除save_article，即移动到save_article_del表中
	 *
	 * @param int $uid
	 * @param string $pkids
	 * @return bool
	 */
	public function del ($uid, $pkids) {
		$uid		= intval($uid);
		$pkids	= format_id_string($pkids, false);		// 返回字符串
		
		// 检查pkids的合法性
		$check = $this->db->execute('
			select
				id
			from
				save_article
			where
				id 			IN ('.$pkids.') and 
				user_id		= "'.$uid.'"
		');
		if (is_array($check)) {
			$pkids = '';
			foreach ($check as $v) {
				$pkids .= $v['id'].',';			// 重构id字符串
			}
			$pkids = substr($pkids, 0, -1);
		} else {
			$pkids = false;
		}
		
		if ($uid and $pkids) {
			$copy_to_del = $this->db->execute('
				insert into
					save_article_del
				(id, user_id, url_id, title, dt, `status`, dt_run)
					select
						id, user_id, url_id, title, dt, 0, "0000-00-00 00:00:00"
					from
						save_article 
					where 
						id 	IN ('.$pkids.')
			');
			if ($copy_to_del) {
				$del = $this->db->execute('
					delete from
						save_article 
					where 
						id 	IN ('.$pkids.')
				');
				if ($del) {
					return $del;
				} else return false;
			} else return false;
		} else return false;
	}
	public function event_del ($article_ids) {
		// 删除circle article记录，查询出涉及到的circle_id，以更新统计数据
		$circles = $this->db->execute('
			select
				circle_id
			from
				circle_article
			where
				article_id IN ('.$article_ids.')
			group by
				circle_id
		');
		if (is_array($circles)) {
			// 删除记录
			$this->db->execute('
				delete from
					circle_article
				where
					article_id IN ('.$article_ids.')
			');
			// 更新circle统计数
			$data_circle = new data_circle();
			foreach ($circles as $v) {
				$data_circle->count_article($v['circle_id']);
			}
		}
		// 删除recommend记录
		$data_recommend_article = new data_recommend_article();
		$data_recommend_article->event_del_by_save_article($article_ids);
		// 删除feed记录
		$data_feed = new data_feed();
		$data_feed->event_del_by_save_article($article_ids);
		// 删除clip记录
		$this->db->execute('
			delete from
				content_clip
			where
				id IN ('.$article_ids.')
		');
		return true;
	}
	public function ls_saved_by_visitor ($uid, $page=1, $limit=20) {
		$uid		= intval($uid);
		$page  	= ($page > 0)  ? intval($page)  : 1;
		$limit 	= ($limit > 0) ? intval($limit) : 1;
		
		if ($uid) {
			$execute = $this->db->execute('
				select
					circle_article.id			AS id,
					save_article.id			AS article_id,
					save_article.user_id		AS user_id,
					save_article.url_id			AS url_id,
					save_article.title			AS title,
					save_article.dt			AS dt,
					save_article.summary		AS summary,
					save_article.img_0			AS img_0,
					save_article.img_1			AS img_1,
					save_article.img_2			AS img_2,
					article_url.url			AS url
				from
					circle_article, circle, save_article, article_url
				where
					circle_article.author_id 	= "'.$uid.'"				and
					circle.id			= circle_article.circle_id		and
					circle.privacy		= "public"				and
					save_article.id		= circle_article.article_id		and
					article_url.id		= save_article.url_id
				group by
					save_article.id
				order by
					save_article.id DESC
				limit
					'.(($page-1)*$limit).', '.$limit.'
			');
			
			if (is_array($execute)) {
				foreach ($execute as $k=>$v) {
					$execute[$k]['circles'] = $this->db->execute('
						select
							circle.id,
							circle.title
						from
							circle_article, circle_member, circle
						where
							circle_article.article_id		= "'.$v['article_id'].'"		and
							circle_member.user_id		= "'.$uid.'"				and
							circle_member.circle_id		= circle_article.circle_id		and
							circle.id				= circle_article.circle_id		and
							circle.privacy			= "public"
					');
				}
				return $execute;
			} else return false;
		} else return false;
	}
	public function ls_in_circle ($circle_id, $page=1, $limit=20) {
		// 提前检查circle的访问权限
		$circle_id	= intval($circle_id);
		$page	= ($page > 0)  ? intval($page)  : 1;
		$limit		= ($limit > 0) ? intval($limit) : 1;
		
		if ($circle_id) {
			$execute = $this->db->execute('
				select
					circle_article.id			AS id,
					save_article.id			AS article_id,
					save_article.user_id		AS user_id,
					save_article.url_id			AS url_id,
					save_article.title			AS title,
					save_article.summary		AS summary,
					save_article.img_0			AS img_0,
					save_article.img_1			AS img_1,
					save_article.img_2			AS img_2,
					circle_article.dt			AS dt,
					article_url.url			AS url,
					users.nickname			AS user_nickname
				from
					circle_article, save_article, article_url, users
				where
					circle_article.circle_id		= "'.$circle_id.'"			and
					save_article.id			= circle_article.article_id		and
					article_url.id			= save_article.url_id		and
					users.id				= save_article.user_id
				order by
					circle_article.id		DESC
				limit
					'.(($page-1)*$limit).', '.$limit.'
			');
			return $execute;
		} else return false;
	}
	/**
	 * 获取圈子中的一篇文章的详情
	 *		public circle 或者 circle 成员可以返回结果
	 * 
	 * @param int $uid			// uid 可以为0
	 * @param int $circle_article_id	// circle_article的主键id
	 * @return array
	 */
	public function one_in_circle ($uid, $circle_article_id) {
		$uid			= intval($uid);
		$circle_article_id	= intval($circle_article_id);
		
		if ($circle_article_id) {
			$article = $this->db->execute('
				select
					circle_article.id			AS id,
					circle_article.dt			AS dt,
					save_article.id			AS article_id,
					save_article.id			AS content_id,
					save_article.user_id		AS user_id,
					save_article.title			AS title,
					article_url.id			AS url_id,
					article_url.url			AS url,
					circle.id				AS circle_id
				from
					circle_article, save_article, article_url, circle
				where
					circle_article.id		= "'.$circle_article_id.'"		and
					circle.id			= circle_article.circle_id		and
					(circle.privacy = "public" or circle.id IN (select circle_member.circle_id from circle_member where circle_member.user_id="'.$uid.'" and (circle_member.type = "member" or circle_member.type = "admin" or circle_member.type = "founder"))) and
					save_article.id		= circle_article.article_id		and
					article_url.id		= save_article.url_id
			');
			if ($article) {
				return $article[0];
			} else return false;
		} else return false;
	}
	/**
	 * 获取存档的文档详情
	 * 		只有本人才能查看
	 *
	 * @param int $uid
	 * @param int $article_id			// 在save_article表中的主键id
	 * @return array
	 */
	public function one ($uid, $article_id) {
		$uid		= intval($uid);
		$article_id 	= intval($article_id);

		if ($uid and $article_id) {
			$execute = $this->db->execute('
				select
					save_article.id		AS id,
					save_article.id		AS article_id,
					save_article.id		AS content_id,
					save_article.user_id	AS user_id,
					save_article.title		AS title,
					save_article.dt		AS dt,
					article_url.id		AS url_id,
					article_url.url		AS url
				from
					save_article, article_url
				where
					save_article.id		= "'.$article_id.'"			and
					save_article.user_id	= "'.$uid.'"				and
					article_url.id		= save_article.url_id
			');
			if ($execute) {
				return $execute[0];
			} else return false;
		} else return false;
	}
	public function is_public ($article_id) {
		$article_id = intval($article_id);
		
		if ($article_id) {
			$execute = $this->db->execute('
				select
					circle_article.id
				from
					circle_article, circle
				where
					circle_article.article_id	= "'.$article_id.'"			and
					circle.id			= circle_article.circle_id		and
					circle.privacy		= "public"
				limit
					1
			');
			if ($execute) {
				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 状态：
	 *	0 = content中的下载大图和analyse(content)=>(clip,summary,bigimg)都需要做
	 *	1 = content中的下载大图还需再次检查，analyse已完成，但是如果有大图更新，还需要重新analyse(content)=>(clip,summary,bigimg)
	 *	2 = content中的下载大图完成，analyse完成
	 *	3 = clip被修改了，需要检查clip中的大图和analyse(clip)=>(summary, bigimg)
	 *	4 = clip中的下载大图还需再次检查，analyse已完成，但是如果有大图更新，还需要重新analyse(clip)=>(summary, bigimg)
	 *	5 = clip中的下载大图完成，analyse完成
	 * @param type $article_id
	 * @param type $status
	 * @return boolean
	 */
	private function set_is_clip_over ($article_id, $status) {
		$article_id	= intval($article_id);
		$status	= intval($status);
		
		if ($article_id) {
			return $this->db->execute('
				update
					save_article
				set
					is_clip_over = "'.$status.'"
				where
					id = "'.$article_id.'"
			');
		} else return false;
	}
	private function get_is_clip_over ($article_id) {
		$execute = $this->db->execute('
			select
				is_clip_over
			from
				save_article
			where
				id = "'.$article_id.'"
		');
		if ($execute) {
			return intval($execute[0]['is_clip_over']);
		} else return false;
	}
	
	/**
	 * 在autorun中运行
	 *	
	 * @param type $article_id
	 * @return boolean
	 */
	public function auto_clip_by_event ($article_id) {
		$article_id = intval($article_id);
		
		if ($article_id) {
			$is_clip_over = $this->get_is_clip_over($article_id);	// Int
			
			if ($is_clip_over === 0) {		// 第一次运行clip。0 = 下载大图和analyse都需要做
				// 获取正文
				$content = $this->get_save_article_content($article_id);
				$content_clip = new content_clip();
				// 下载大图并替换到原html中
				$new_content = $content_clip->download_big_img_and_replace($content);
				if ($new_content) {
					$this->set_save_article_content($article_id, $new_content);
				} else {
					$new_content = $content;
				}
				$this->analyse_and_save_and_set_status_to_1($article_id, $new_content);		// 分析、保存结果、标记状态为1
				return true;
			} elseif ($is_clip_over === 1) {		// 第二次运行clip。1 = 大图还需再次检查，analyse已完成，但是如果有大图更新，还需要重新analyse
				// 获取正文
				$content = $this->get_save_article_content($article_id);
				$content_clip = new content_clip();
				// 下载大图并替换到原html中
				$new_content = $content_clip->download_big_img_and_replace($content);
				if ($new_content) {
					$this->set_save_article_content($article_id, $new_content);		// 还有下载大图的更新，保存更新
					$this->analyse_and_save_and_set_status_to_1($article_id, $new_content);		// 分析、保存结果、标记状态为1
					return true;
				} else {
					// 更新is_clip_over状态
					$this->set_is_clip_over($article_id, 2);		// 已经没有需要更新的了，分析完毕
					return true;
				}
			} elseif ($is_clip_over === 2) {		// 已经完成，不用再clip了。2 = 大图下载完成，analyse完成
				return true;
			} elseif ($is_clip_over === 3) {		// 3 = clip被修改了，需要检查clip中的大图和analyse(clip)=>(summary, bigimg)
				// 获取clip
				$clip = $this->get_content_clip($article_id);
				$content_clip = new content_clip();
				// 下载大图并替换到原clip中
				$new_clip = $content_clip->download_big_img_and_replace($clip);
				if ($new_clip) {
					$this->set_content_clip($article_id, $new_clip);
				} else {
					$new_clip = $clip;
				}
				$this->analyse_and_save_and_set_status_to_4($article_id, $new_clip);		// 分析clip、保存summary和bigimg、标记状态为4
				return true;
			} elseif ($is_clip_over === 4) {		// 4 = clip中的下载大图还需再次检查，analyse已完成，但是如果有大图更新，还需要重新analyse(clip)=>(summary, bigimg)
				// 获取clip
				$clip = $this->get_content_clip($article_id);
				$content_clip = new content_clip();
				// 下载大图并替换到原clip中
				$new_clip = $content_clip->download_big_img_and_replace($clip);
				if ($new_clip) {
					$this->set_content_clip($article_id, $new_clip);
					$this->analyse_and_save_and_set_status_to_4($article_id, $new_clip);		// 分析clip、保存summary和bigimg、标记状态为4
					return true;
				} else {
					// 更新is_clip_over状态
					$this->set_is_clip_over($article_id, 5);		// 已经没有需要更新的了，分析完毕
					return true;
				}
			} elseif ($is_clip_over === 5) {		// 5 = clip中的下载大图完成，analyse完成
				return true;
			} else {
				return false;
			}
		} else return false;
	}
	private function analyse_and_save_and_set_status_to_1 ($article_id, $content) {
		$content_clip = new content_clip();
		// 获取url
		$execute = $this->db->execute('select article_url.url from save_article, article_url where save_article.id	= "'.$article_id.'" and article_url.id = save_article.url_id');
		// 分析clip
		$clip = $content_clip->analyse($content, $execute[0]['url']);			// (clip, summary, big_imgs[0,1,2])
		// 记录clip，即使为空也要写入
		$this->set_content_clip($article_id, $clip['clip']);
		// 记录summary和img
		$this->save_summary_and_img($article_id, $clip['summary'], $clip['big_imgs'][0], $clip['big_imgs'][1], $clip['big_imgs'][2]);
		// 更新is_clip_over状态
		$this->set_is_clip_over($article_id, 1);		// 更新状态：大图还需检查
	}
	private function analyse_and_save_and_set_status_to_4 ($article_id, $clip) {
		$content_clip = new content_clip();
		// 获取url
		$execute = $this->db->execute('select article_url.url from save_article, article_url where save_article.id	= "'.$article_id.'" and article_url.id = save_article.url_id');
		// 分析clip
		$clip = $content_clip->analyse($clip, $execute[0]['url']);			// (clip, summary, big_imgs[0,1,2])
		// 这里不用记录clip了
		// 记录summary和img
		$this->save_summary_and_img($article_id, $clip['summary'], $clip['big_imgs'][0], $clip['big_imgs'][1], $clip['big_imgs'][2]);
		// 更新is_clip_over状态
		$this->set_is_clip_over($article_id, 4);		// 更新状态：大图还需检查
	}
	private function save_summary_and_img ($article_id, $summary, $img_0, $img_1, $img_2) {
		return $this->db->execute('
			update
				save_article
			set
				summary	= "'.addslashes($summary).'",
				img_0	= "'.addslashes($img_0).'",
				img_1	= "'.addslashes($img_1).'",
				img_2	= "'.addslashes($img_2).'"
			where
				id = "'.$article_id.'"
		');
	}
}
?>